
package com.yimixian.app.address;

import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.search.core.PoiInfo;
import com.baidu.mapapi.search.geocode.ReverseGeoCodeResult;
import com.umeng.analytics.MobclickAgent;
import com.yimixian.app.EventBusDomain.ShowAdSelectChange;
import com.yimixian.app.R;
import com.yimixian.app.activity.YMXMainActivity;
import com.yimixian.app.common.CacheKeys;
import com.yimixian.app.common.Constants;
import com.yimixian.app.common.SharePreferenceKeys;
import com.yimixian.app.common.StatConstants;
import com.yimixian.app.common.YmxTitleBarActivity;
import com.yimixian.app.data.DataManager;
import com.yimixian.app.model.Address;
import com.yimixian.app.user.UserRegistrationActivity;
import com.yimixian.app.util.SharedPreferencesHelper;
import com.yimixian.app.util.Strings;

import de.greenrobot.event.EventBus;
import rx.functions.Action1;

import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;

public abstract class AbstractAddressActivity extends YmxTitleBarActivity {
    public int mMode;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (savedInstanceState != null) {
            mMode = savedInstanceState.getInt(ShowAddressesActivity.EXTRA_MODE,
                    ShowAddressesActivity.LOCATE_ADDRESS_MODE);
            setTabIndex(savedInstanceState);
        } else {
            mMode = getIntent().getIntExtra(ShowAddressesActivity.EXTRA_MODE,
                    ShowAddressesActivity.LOCATE_ADDRESS_MODE);
        }
        if (savedInstanceState == null && DataManager.getInstance().get(CacheKeys.TOKEN) == null) {
            Intent intent = new Intent(this, UserRegistrationActivity.class);
            startActivityForResult(intent, Constants.USER_REGISTRATION_REQUEST_CODE);
        } else {
            resumeOnCreate();
        }
    }

    public void setAddressViewListener(AddressesView mAddressesView,
            final DataManager mDataManager, final boolean isSaveStoreType) {
        mAddressesView.setListener(new AddressesView.Listener() {
            @Override
            public void onAddressClicked(Address address) {
                MobclickAgent.onEvent(AbstractAddressActivity.this,
                        StatConstants.CHOOSE_ADDRESS_ITEM_ADDRESS_CLICK);
                stopLocating();
                if (address.id > 0 && Strings.isNullOrEmpty(address.poiId) && Strings.isNullOrEmpty(address.storeId)) {
                    Intent intent = new Intent(AbstractAddressActivity.this,
                            ModifyOrCreateAddressActivity.class);
                    intent.putExtra(ModifyOrCreateAddressActivity.EXTRA_ADDRESS_ID, address.id);
                    startActivityForResult(intent, Constants.MODIFY_ADDRESS_REQUEST_CODE);
                    return;
                }

                //                if (isSaveStoreType) {
                //                    mDataManager.put(CacheKeys.LAST_SAVED_STORE_TYPE,
                //                            YMXMainActivity.SAVED_STORE_TYPE_ADDRESS);
                //                }
                SharedPreferencesHelper.saveAddress(address, SharePreferenceKeys.CURRENT_ADDRESS);
                EventBus.getDefault().postSticky(new ShowAdSelectChange());
                if (mMode == ShowAddressesActivity.MANAGE_ADDRESS_MODE) {//从地址管理页面跳转过来
                    startActivity(YMXMainActivity.buildIntentTab(AbstractAddressActivity.this,
                            YMXMainActivity.TAB_HOME));
                } else {//从首页直接跳转过来
                    setResult(RESULT_OK);
                }
                finish();
            }

            @Override
            public void onAddressEditIconClicked(Address address) {
                MobclickAgent.onEvent(AbstractAddressActivity.this,
                        StatConstants.CHOOSE_ADDRESS_ITEM_ADDRESS_EDIT_CLICK);
                stopLocating();
                Intent intent = new Intent(AbstractAddressActivity.this,
                        ModifyOrCreateAddressActivity.class);
                intent.putExtra(ModifyOrCreateAddressActivity.EXTRA_ADDRESS_ID, address.id);
                startActivityForResult(intent, Constants.MODIFY_ADDRESS_REQUEST_CODE);
            }
        });
    }


    public void getAction1(LatLng latLng, PoiInfo poiInfo) {
        if (poiInfo == null) {
            onLocatingFailed();
            return;
        }
        ShortAddress shortAddress = new ShortAddress(poiInfo, latLng);
        SharedPreferencesHelper.saveAddress(shortAddress, SharePreferenceKeys.CURRENT_ADDRESS);
//        SharedPreferencesHelper.saveAddress(shortAddress, SharePreferenceKeys.CURRENT_POSITION);
        EventBus.getDefault().postSticky(new ShowAdSelectChange());
        setResult(RESULT_OK);
        finish();
    }

    public void getErrorAction1() {
        onLocatingFailed();
    }

//    public Action1 getAction1(final DataManager mDataManager) {
//        return new Action1<ReverseGeoCodeResult>() {
//            @Override
//            public void call(ReverseGeoCodeResult reverseGeoCodeResult) {
//                if (reverseGeoCodeResult == null) {
//                    onLocatingFailed();
//                    return;
//                }
//                PoiInfo poiInfo = reverseGeoCodeResult.getPoiList().get(0);
//                ShortAddress shortAddress = new ShortAddress(poiInfo, poiInfo.location);
//                SharedPreferencesHelper.saveAddress(shortAddress, SharePreferenceKeys.CURRENT_ADDRESS);
//                SharedPreferencesHelper.saveAddress(shortAddress, SharePreferenceKeys.CURRENT_POSITION);
//                EventBus.getDefault().postSticky(new ShowAdSelectChange());
//                setResult(RESULT_OK);
//                finish();
//            }
//        };
//    }

//    public Action1 getErrorAction1() {
//        return new Action1<Throwable>() {
//            @Override
//            public void call(Throwable throwable) {
//                onLocatingFailed();
//            }
//        };
//    }

    public void onREGResult(int requestCode, int resultCode) {
        if (requestCode == Constants.USER_REGISTRATION_REQUEST_CODE) {
            if (resultCode == RESULT_OK) {
                resumeOnCreate();
            } else {
                finish();
                overridePendingTransition(0, 0);
            }
        }
    }

    abstract public void setTabIndex(Bundle savedInstanceState);

    abstract public void resumeOnCreate();

    abstract public void stopLocating();

    abstract public void onLocatingFailed();

}
